const KoaRouter = require('@koa/router')
const { auth } = require('../middleware/auth.middleware')
const { createAddr, findAll, updateAddr, removeAddrById, setDefault } = require('../controller/addr.controller')
const { addrValidator } = require('../middleware/addr.middleware')

/**地址模块路由 */
const addrRouter = new KoaRouter({ prefix: '/address' })
/**添加地址 */
addrRouter.post('/', auth, addrValidator({
  consignee: 'string',
  phone: { type: 'string', format: /^1\d{10}$/ },
  address: 'string'
}), createAddr)
/**获取地址列表 */
addrRouter.get('/', auth, findAll)
/**修改地址 */
addrRouter.put('/:id', auth, addrValidator({
  consignee: 'string',
  phone: { type: 'string', format: /^1\d{10}$/ },
  address: 'string'
}), updateAddr)
/**删除地址 */
addrRouter.delete('/:id', auth, removeAddrById)
/**设置默认地址 */
addrRouter.patch('/:id', auth, setDefault)
module.exports = addrRouter